package com.example.jdk8;

import sun.misc.BASE64Encoder;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class myBase64 {
    public static void main(String[] args) {

        String orig = "hello world!";
        String desc = Base64.getEncoder().encodeToString(orig.getBytes(StandardCharsets.UTF_8));
        System.out.println("加密后的字符串为:"+desc);

        String unDecodeStr=new String(Base64.getDecoder().decode(desc),StandardCharsets.UTF_8);
        System.out.println("解密后的字符串为"+unDecodeStr);

        String encodedSun;
        byte[] randomBinaryData = new byte[5000000];
        long durationSun = 0;
        long durationJava8 = 0;
        long durationCommons = 0;
        byte[] encodedJava8;
        byte[] encodedCommons;

        //testSunBase64Encode
        BASE64Encoder encoder = new BASE64Encoder();
        long before = System.currentTimeMillis();
        encodedSun = encoder.encode(randomBinaryData);
        long after = System.currentTimeMillis();
        durationSun = after-before;
        System.out.println("Sun: " + durationSun);


        //testJava8Base64Encode
        before = System.currentTimeMillis();
        Base64.Encoder encoder2 = Base64.getEncoder();
        encodedJava8 = encoder2.encode(randomBinaryData);
        after = System.currentTimeMillis();
        durationJava8 = after-before;
        System.out.println("Java8: " + durationJava8);


        before = System.currentTimeMillis();
        encodedCommons = org.apache.commons.codec.binary.Base64.encodeBase64(randomBinaryData);
        after = System.currentTimeMillis();
        durationCommons = after-before;
        System.out.println("Commons: " + durationCommons);
    }
}
